package com.mydroidchat.data;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {
    public static final String URL_START = "http://mydroid.by/apichat.php";
    public static final Pattern UNESCAPE_UNICODE = Pattern.compile("\\\\u([0-9A-Fa-f]{4})");

    public static final String getMd5Hash(String pass) {
        String result = "";
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] mdbytes = md5.digest(pass.getBytes("UTF-8"));

            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < mdbytes.length; i++) {
                sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            result = sb.toString();
        } catch (NoSuchAlgorithmException e) {
        } catch (UnsupportedEncodingException e) {
        }
        return result;
    }

    public static final String unescapeUnicode(String message) {
        Matcher matcher1 = UNESCAPE_UNICODE.matcher(message);
        while (matcher1.find()) {
            message = matcher1.replaceFirst(String.format("%c", (char) Integer.parseInt(matcher1.group(1), 16)));
            matcher1 = UNESCAPE_UNICODE.matcher(message);
        }
        return message;
    }
}
